package cn.donting.jme.edit;

import cn.donting.jme.edit.scene.J3E;
import cn.donting.jme.edit.scene.J3EBuilder;
import cn.donting.jme.edit.state.CanvasSyncAppState;
import cn.donting.jme.edit.state.SceneMoveAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.app.StatsAppState;
import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.debug.Grid;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeContext;
import javafx.scene.canvas.Canvas;
import lombok.Getter;

import java.io.IOException;

/**
 * jme场景视图
 */
@Getter
public class JmeEditSceneApp extends SimpleApplication {
    private Canvas canvas;

    /**
     * 场景节点
     */
    private Node sceneNode;
    /**
     * 编辑器节点
     * transform 轴
     */
    private Node editorNode = new Node("编辑器节点");

    /**
     * 编辑场景的摄像机
     * 能看 rootNode 默认相机
     */
    private Camera editCamera;

    /**
     * 正在编辑的j3e 文件
     */
    private J3E j3E;

    /**
     * @param canvas fx Canvas
     */
    public JmeEditSceneApp(Canvas canvas) {
        super(new StatsAppState());
        this.canvas = canvas;
        settings = new AppSettings(true);
        //设置大一点，否则会出现 不能放大的情况
        //InputLwjglOffscreenBuffer 回创建一个不可见的window 对象，不可改变大小
        settings.setWidth(1920);
        settings.setHeight(1080);
        settings.setWidth((int) canvas.getWidth());
        settings.setHeight((int) canvas.getHeight());
    }

    /**
     * 初始化 编辑器节点
     * 添加编辑器节点灯光等
     */
    private void initEditorNode() {
        AmbientLight ambientLight = new AmbientLight();
        editorNode.addLight(ambientLight);
        Grid grid = new Grid(100, 100, 1);
        Geometry geometry = new Geometry("", grid);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        geometry.setMaterial(mat);
        editorNode.attachChild(geometry);
        geometry.move(-50, 0, -50);
    }

    /**
     * 打开一个j3e 文件
     *
     * @param j3E
     */
    public void openJ3E(J3E j3E) {
        this.j3E = j3E;
        Node sceneNode = j3E;
        this.sceneNode = sceneNode;
        rootNode.attachChild(this.sceneNode);
    }

    @Override
    public void simpleInitApp() {

        Camera camera = getCamera();
        editCamera = camera;
        editCamera.setLocation(editCamera.getLocation().add(new Vector3f(0, 2, 0)));
        CanvasSyncAppState canvasSyncState = new CanvasSyncAppState(canvas);
        stateManager.attach(canvasSyncState);

        SceneMoveAppState sceneMoveAppState = new SceneMoveAppState();
        stateManager.attach(sceneMoveAppState);

//        CoordinateViewAppState coordinateViewAppState = new CoordinateViewAppState();
//        stateManager.attach(coordinateViewAppState);


        rootNode.attachChild(editorNode);
        rootNode.attachChild(sceneNode);

        initEditorNode();
        reshape((int) canvas.getWidth(), (int) canvas.getHeight());
    }

    public Material loadMaterial(String defName) {
        Material mat = new Material(assetManager, defName);
        return mat;
    }


    @Override
    public void destroy() {
        super.destroy();
        try {
            J3EBuilder.saveJ3E(j3E);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
